package com.xbongbong.workflow.service.operator.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.formula.constant.Constant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.PaasDateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.paas.pojo.workflow.WorkflowDataPojo;
import com.xbongbong.workflow.pojo.WorkflowOperationsPojo;
import com.xbongbong.workflow.service.operator.WorkflowOperatorFieldHandler;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/4 21:11
 * @since v1.0
 */
@Service("workflowOperatorDateTimeHandlerImpl")
public class WorkflowOperatorDateTimeHandlerImpl implements WorkflowOperatorFieldHandler {

    @Override
    public List<Integer> type() {
        return Arrays.asList(FieldTypeEnum.DATETIME.getType());
    }


    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo) throws XbbException {
        return data.get(fieldAttrEntity.getAttr());
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo,
                                         WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        // 当目标表单为日期字段而当前表单为日期时间字段，此时将当前表单中日期时间字段件中的日期赋值给目标表单中的日期字段，丢弃时间。
        // 当目标表单为日期时间字段而当前表单为日期字段，此时将当前表单中日期字段中的日期赋值给目标表单中的日期时间字段，时间设置为00:00:00
        String attr = fieldAttrEntity.getAttr();
        if (Objects.equals(targetField.getDateType(), DateTimeEnum.TIME.getType())) {
            Long time = data.getLong(attr);
            if (Objects.equals(0L, time)) {
                return null;
            }
            Long beforeDate = PaasDateUtil.getBeforeDayBreak(data.getLong(attr));
            if (Objects.equals(0L, beforeDate)) {
                return null;
            }
            return beforeDate;
        } else {
            return data.get(attr);
        }
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        if (Objects.equals(DateTimeEnum.TIME.getType(), targetField.getDateType())) {
            Long beforeDate = PaasDateUtil.getBeforeDayBreak(Long.valueOf(String.valueOf(workflowOperationsPojo.getValue())));
            if (Objects.equals(0L, beforeDate)) {
                return null;
            }
            return beforeDate;
        }
        return workflowOperationsPojo.getValue();
    }

    @Override
    public Object formulaHandlerThisField(FieldAttrEntity fieldAttrEntity, PaasFormDataEntity formDataEntity, JSONObject data, Long formId, Integer businessType,  String corpid) throws XbbException {

        Object value = data.get(fieldAttrEntity.getAttr());
        if (Objects.isNull(value)) {
            value = 0L;
        }
        return value;
    }

    @Override
    public Object formulaHandlerTargetField(Object value, WorkflowOperationsPojo workflowOperationsPojo, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, String corpid) throws XbbException {
        if (Objects.equals(value, 0L) || Objects.equals(value, 0) || StringUtil.isEmpty(value.toString()) || Objects.equals(value.toString(), Constant.ZERO_STRING)) {
            return null;
        }
        Long time = Long.valueOf(value.toString()) * 1000L;
        if (Objects.equals(DateTimeEnum.DATETIME.getType(), targetField.getDateType())) {
            return DateTimeUtil.getInt(DateTimeUtil.getString(time, DateTimeUtil.SDF), DateTimeUtil.SDFYMDHM);
        } else {
            return DateTimeUtil.getInt(DateTimeUtil.getString(time, DateTimeUtil.SDF), DateTimeUtil.SDFDate);
        }
    }
}
